<template>
    <el-dialog
      :visible.sync="visible"
      :title="dataForm.details ? '详情' :(!dataForm.id ? $t('add') : $t('update'))"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="close()"
      :destroy-on-close="true"
    >
      <el-form
        :model="dataForm"
        :rules="dataRule"
        ref="dataForm"
        @keyup.enter.native="dataFormSubmitHandle()"
        :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'"
        label-position="right"
        :disabled="disabled"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="区域名称" prop="nameIsland">
                <el-input type="text" v-model="dataForm.nameIsland" :placeholder="disabled?'':'区域名称'"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="停车场名称" prop="parkadeName">
                <el-input type="text" v-model="dataForm.parkadeName" :placeholder="disabled?'':'停车场名称'"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="停车场地址" prop="coordinate">
                <el-input type="text" v-model="dataForm.coordinate" :placeholder="disabled?'':'停车场地址'"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="预约规则" prop="reservationRule">
                <el-input type="textarea" v-model="dataForm.reservationRule" :placeholder="disabled?'':'预约规则'"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="收费标准" prop="chargeStandard">
                <el-input type="textarea" v-model="dataForm.chargeStandard" :placeholder="disabled?'':'收费标准'"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="经度" prop="longitude">
                <el-input type="number" v-model="dataForm.longitude" :placeholder="disabled?'':'经度'"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬度" prop="latitude">
                <el-input type="number" v-model="dataForm.latitude" :placeholder="disabled?'':'纬度'"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="区域号" prop="region">
                <el-input type="text" v-model="dataForm.region" :placeholder="disabled?'':'区域号'"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车位总数" prop="eachAllParking">
                <el-input type="number" v-model="dataForm.eachAllParking" :placeholder="disabled?'':'车位总数'"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template slot="footer" v-if="!disabled">
        <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
        <el-button type="primary" @click="dataFormSubmitHandle()">{{
          $t("confirm")
        }}</el-button>
      </template>
    </el-dialog>
  </template>
  
  <script>
  import debounce from "lodash/debounce";
  import mixinRegionModule from "@/mixins/region-module";
  
  export default {
    mixins: [mixinRegionModule],
  
    data() {
      return {
        visible: false,
        dataForm: {
          id: "",
          nameIsland:"",
          parkadeName:"",
          coordinate:"",
          reservationRule:"",
          chargeStandard:"",
          longitude:"",
          latitude:"",
          region:'',
          eachAllParking:""
        },
      };
    },
    props: { disabled: Boolean },
    computed: {
      dataRule() {
        return {
            nameIsland: [
                { required: true, message: "请输入所属区域", trigger: "blur" },
            ],
            parkadeName: [
                { required: true, message: "请输入停车场名称", trigger: "blur" },
            ],
            coordinate: [
                { required: true, message: "请输入停车场地址", trigger: "blur" },
            ],
            longitude: [
                { required: true, message: "请输入停车场经度", trigger: "blur" },
            ],
            latitude: [
                { required: true, message: "请输入停车场纬度", trigger: "blur" },
            ],
            region: [
                { required: true, message: "请输入区域号", trigger: "blur" },
            ],
            eachAllParking: [
                { required: true, message: "请输入停车场车位总数", trigger: "blur" },
            ]
        };
      },
    },
    methods: {
      close() {
        this.dataForm = {
          id: "",
          nameIsland:"",
          parkadeName:"",
          coordinate:"",
          reservationRule:"",
          chargeStandard:"",
          longitude:"",
          latitude:"",
          region:'',
          eachAllParking:""
        };
        this.$emit("changeDisabled");
      },
  
      init() {
        this.visible = true;
        this.$nextTick(() => {
          this.$refs["dataForm"].resetFields();
          if (this.dataForm.id) {
            this.getInfo();
          }
        });
      },
      // 获取信息
      getInfo() {
        this.$http
          .get("/travel/car/wharfStopParkade/" + this.dataForm.id)
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            }
            this.dataForm = {
              ...this.dataForm,
              ...res.data,
            };
          })
          .catch(() => {});
      },
      // 表单提交
      dataFormSubmitHandle: debounce(
        function () {
          this.$refs["dataForm"].validate((valid) => {
            if (!valid) {
              return false;
            }

            this.$http[!this.dataForm.id ? "post" : "put"](
              !this.dataForm.id ? "/travel/car/wharfStopParkade" : "/travel/car/wharfStopParkade",
              this.dataForm
            )
              .then(({ data: res }) => {
                if (res.code !== 0) {
                  return this.$message.error(res.msg);
                }
                this.$message({
                  message: this.$t("prompt.success"),
                  type: "success",
                  duration: 500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList");
                  },
                });
              })
              .catch(() => {});
          });
        },
        1000,
        { leading: true, trailing: false }
      ),
    }
  };
  </script>
  